package com.yubiaoqing;

import com.intellij.execution.filters.TextConsoleBuilderFactory;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.ui.content.Content;

/**
 * @author hehb
 * @date 2020/04/04 14:49
 */
public class MyToolWindow {

    private ToolWindow toolWindow;
    private ConsoleView consoleView;

    private static MyToolWindow instance;

    private MyToolWindow(ToolWindow toolWindow) {
        this.toolWindow = toolWindow;
    }

    public void activate(Project project) {
        if (consoleView == null) {
            consoleView = TextConsoleBuilderFactory.getInstance().createBuilder(project).getConsole();
            Content content = toolWindow.getContentManager().getFactory().createContent(consoleView.getComponent(), "Console", true);
            this.toolWindow.getContentManager().addContent(content);
        }
        this.toolWindow.activate(() -> {

        });
    }

    public void println(String text) {
        consoleView.print(
                text + System.lineSeparator(), ConsoleViewContentType.NORMAL_OUTPUT);
    }

    public static void init(ToolWindow toolWindow) {
        if (instance == null)
            instance = new MyToolWindow(toolWindow);
    }

    public static MyToolWindow getInstance() {
        return instance;
    }
}
